package com.company.manage.system.web.controller;

import com.company.manage.common.base.BaseController;
import com.company.manage.common.persistence.Page;
import com.company.manage.common.persistence.PropertyFilter;
import com.company.manage.system.entity.Dict;
import com.company.manage.system.service.DictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by HuangYong on 2016/1/23.
 * desc:字典controller
 */
@Controller
@RequestMapping("/system/dict")
public class DictController extends BaseController {
    private static final String TAG = "DictController";

    @Autowired
    private DictService dictService;

    /**
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public String list() {
        return "system/dictList";
    }


    /**
     * 获取字典json
     *
     * @param request
     * @return
     */
    @RequiresPermissions("sys:dict:view")
    @RequestMapping(value = "json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> dictList(HttpServletRequest request) {
        Page<Dict> page = getPage(request);

        List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);

        page = dictService.search(page, filters);

        return getEasyUIData(page);
    }

    /**
     * 添加字典跳转
     *
     * @param model
     * @return
     */
    @RequiresPermissions("sys:dict:add")
    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String createForm(Model model) {
        model.addAttribute("dict", new Dict());
        model.addAttribute("action", "create");
        return "system/dictForm";
    }

    /**
     * 添加数据字典
     *
     * @param dict
     * @param model
     * @return
     */
    @RequiresPermissions("sys:dict:add")
    @RequestMapping(value = "create",method = RequestMethod.POST)
    public String create(@Valid Dict dict, Model model) {
        dictService.save(dict);
        return "success";
    }

    /**
     * 修改数据字典 跳转
     * @param id
     * @param model
     * @return
     */
    @RequiresPermissions("sys:dict:update")
    @RequestMapping(value = "update/{id}",method = RequestMethod.GET)
    public String updateForm(@PathVariable("id") Integer id,Model model){
        model.addAttribute("dict",dictService.get(id));
        model.addAttribute("action","update");
        return "system/dictForm";
    }

    /**
     * 修改数据字典
     * @param dict
     * @param model
     * @return
     */
    @RequiresPermissions("sys:dict:update")
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public String update(@Valid @ModelAttribute @RequestBody Dict dict,Model model){
        dictService.save(dict);
        return "success";
    }

    /**
     * 删除数据字典
     * @param id
     * @param model
     * @return
     */
    @RequiresPermissions("sys:dict:delete")
    @RequestMapping(value = "delete/{id}",method = RequestMethod.GET)
    @ResponseBody
    public String delete(@PathVariable("id") Integer id,Model model){
        dictService.delete(id);
        return "success";
    }

    @ModelAttribute
    public void getDict(@RequestParam(value = "id",defaultValue = "-1") Integer id,Model model){
        if(id!=-1){
            model.addAttribute("dict",dictService.get(id));
        }
    }
}